﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Web.Security;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.admin
{
    public partial class addAdmin : BaseAdmin
    {

        readonly dalRole theRole = new dalRole();
        // Methods
   

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.BindRoleList();
                if (Request.QueryString["uid"] != AdminUserId)
                {
                      base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddAdmin);
                }
         
                if (base.Request.QueryString["uid"] != null)
                {
                    this.txtLoginName.Readonly = true;
                    this.upbind();
                    this.txtLoginPassword.Required = false;
                    this.txtLoginPassword.ShowRedStar = false;
                    this.txtConfirmPassword.Required = false;
                    this.txtConfirmPassword.ShowRedStar = false;
                }
            }
        }
        private void BindRoleList()
        {
            DataTable table = theRole.GetList(" isnull(isDeleted,0)=0 ").Tables[0];
            this.ddlRoleList.DataTextField = "RoleName";
            this.ddlRoleList.DataValueField = "Id";
            this.ddlRoleList.DataSource = table;
            this.ddlRoleList.DataBind();
            this.ddlRoleList.Items.Insert(0, new ExtAspNet.ListItem("选择角色","-1"));
            this.ddlRoleList.SelectedValue = "-1";
        }

        private void upbind()
        {
            string sql = "select top 1 * from tbAdminLogin where Adminid=" + base.Request.QueryString["uid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.txtLoginName.Text = table.Rows[0]["AdminUser"].ToString();
            this.txtUserName.Text = table.Rows[0]["AdminName"].ToString();

            this.txtRemark.Text = table.Rows[0]["AdminDesc"].ToString();
            if (this.ddlRoleList.Items.FindByValue(table.Rows[0]["RoleId"].ToString()) != null)
            {
                this.ddlRoleList.SelectedValue = table.Rows[0]["RoleId"].ToString();
            }
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["uid"] != null)
            {
                string str2;
                DataTable table = SQLAccess.GreatDs("SELECT AdminPwd FROM tbAdminLogin where Adminid=" + base.Request.QueryString["uid"] + "").Tables[0];
                if (re.replace(this.txtLoginPassword.Text).ToString() == table.Rows[0]["AdminPwd"].ToString())
                {
                    str2 = re.replace(this.txtLoginPassword.Text);
                }
                else
                {
                    str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginPassword.Text), "MD5");
                }
                if (this.txtLoginPassword.Text.Trim() == "")
                {
                    SQLAccess.DoSql("update tbAdminLogin set AdminUser='" + re.replace(this.txtLoginName.Text) + "',AdminName='" + re.replace(this.txtUserName.Text) + "',AdminFlag=1,AdminDesc='" + re.replace(this.txtRemark.Text) + "' ,RoleId=" + this.ddlRoleList.SelectedItem.Value + " where Adminid=" + base.Request.QueryString["uid"] + "");
                }
                else
                {
                    SQLAccess.DoSql("update tbAdminLogin set AdminUser='" + re.replace(this.txtLoginName.Text) + "',AdminPwd='" + str2 + "',AdminName='" + re.replace(this.txtUserName.Text) + "',AdminFlag=1,AdminDesc='" + re.replace(this.txtRemark.Text) + "' ,RoleId=" + this.ddlRoleList.SelectedItem.Value + " where Adminid=" + base.Request.QueryString["uid"] + "");
                }
                ExtAspNet.Alert.Show("修改成功！", "", "openNewTab('AdminList','/admin/admin/adminList.aspx','管理员列表','adminEdit')");

            }
            else
            {
                DataTable table2 = SQLAccess.GreatDs("SELECT AdminId FROM tbAdminLogin where AdminUser='" + re.replace(this.txtUserName.Text) + "'").Tables[0];
                if (table2.Rows.Count > 0)
                {
                    ExtAspNet.Alert.Show("对不起，此用户已存在，请用别的用户名！");
                }
                else
                {
                    SQLAccess.DoSql("insert into tbAdminLogin(AdminUser,AdminPwd,AdminName,AdminFlag,AdminDesc,RoleId) values ('" + re.replace(this.txtLoginName.Text) + "','" + FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginPassword.Text), "MD5") + "','" + re.replace(this.txtUserName.Text) + "',1,'" + re.replace(this.txtRemark.Text) + "'," + this.ddlRoleList.SelectedItem.Value + " )" );
                    ExtAspNet.Alert.Show("添加成功！", "", "openNewTab('AdminList','/admin/admin/adminList.aspx','管理员列表','AddAdmin')");
                }
            }
        }

       
    }
}